package com.tianji.learning.service.impl;

import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.DateUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.po.PointsRecord;
import com.tianji.learning.domain.vo.PointsStatisticsVO;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mapper.PointsRecordMapper;
import com.tianji.learning.mq.message.PointsMessage;
import com.tianji.learning.service.IPointsRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学习积分记录，每个月底清零 服务实现类
 * </p>
 *
 * @author hzy
 * @since 2023-09-13
 */
@Service
public class PointsRecordServiceImpl extends ServiceImpl<PointsRecordMapper, PointsRecord> implements IPointsRecordService {

    /**
     * 更新积分
     *
     * @param message
     * @param type
     */
    @Override
    public void addSign(PointsMessage message, PointsRecordType type) {
        Long userId = message.getUserId();
        Integer points = message.getPoints();

        int maxPoints = type.getMaxPoints();

        //查询单日时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginTime = DateUtils.beginOfDay(now);
        LocalDateTime endTime = DateUtils.endOfDay(now);

        //查询单日该类型分数值
        Integer score = getBaseMapper().selectScore(userId, type, beginTime, endTime);

        //判断是否有积分上限
        if (maxPoints != 0) {
            //如果今日积分达到上限, 不加分
            if (score >= maxPoints) {
                return;
            }
            //判断是否超过限定值
            int allScore = points + score;
            if (allScore > maxPoints) {
                points = maxPoints - score;
            }
        }

        //封装数据,并保存
        PointsRecord pointsRecord = new PointsRecord();
        pointsRecord.setUserId(userId);
        pointsRecord.setPoints(points);
        pointsRecord.setType(type);
        save(pointsRecord);
    }

    /**
     * 查询今日积分情况
     *
     * @return
     */
    @Override
    public List<PointsStatisticsVO> selectPoint() {
        Long userId = UserContext.getUser();

        //今日时间
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime beginTime = DateUtils.beginOfDay(now);
        LocalDateTime endTime = DateUtils.endOfDay(now);

        //分类查询数据库中各类型的积分情况
        List<PointsRecord> vos = getBaseMapper().selectAllScore(userId, beginTime, endTime);
        if (CollUtils.isEmpty(vos)) {
            return CollUtils.emptyList();
        }

        List<PointsStatisticsVO> voList = new ArrayList<>();
        for (PointsRecord vo : vos) {
            PointsStatisticsVO statisticsVO = new PointsStatisticsVO();
            statisticsVO.setType(vo.getType().getDesc());
            statisticsVO.setPoints(vo.getPoints());
            statisticsVO.setMaxPoints(vo.getType().getMaxPoints());
            voList.add(statisticsVO);
        }
        return voList;
    }
}
